package com.cmwa.scbp.business.infodisclosure.investReport.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.scbp.base.mvc.controller.BaseController;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.Result;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.business.infodisclosure.investReport.entity.InvestReportVo;
import com.cmwa.scbp.business.infodisclosure.investReport.service.InvestReportService;
import com.cmwa.scbp.business.infodisclosure.report.entity.ReportInfoVo;
import com.cmwa.scbp.business.infodisclosure.report.service.ReportInfoService;
import com.cmwa.scbp.business.myHome.todo.entity.TodoBizRelVo;
import com.cmwa.scbp.business.myHome.todo.entity.TodoVo;
import com.cmwa.scbp.business.myHome.todo.service.TodoService;
import com.cmwa.scbp.sys.attach.entity.AttachVo;
import com.cmwa.scbp.sys.attach.service.AttachService;
import com.cmwa.scbp.sys.dictionary.entity.DictionaryVo;
import com.cmwa.scbp.sys.msg.util.StringUtil;
import com.cmwa.scbp.utils.cached.DictionaryCached;
import com.google.common.base.Joiner;

import net.sf.json.JSONArray;

/**
 * 投资者报告表控制器
 * 
 * @author ex-zhengqq
 * @date 2019-01-23 13:37:22
 */
@Controller
@RequestMapping("/business/investReport")
public class InvestReportController extends BaseController {

	private static final Log log = LogFactory.getLog(InvestReportController.class);
	
	private final static String JSP_PATH = "business/infoDisclosure/investReport/";
	@Resource
	private InvestReportService investReportService;
	@Resource
	private ReportInfoService reportInfoService;
	@Resource
	private AttachService attachService;
	@Resource
	private TodoService todoService;
		
	/**
	 * 跳转到列表页面
	 * @author ex-zhengqq
	 * @date 2019-01-23 13:37:22
	 * @param model
	 * @return
	 */
	@RequestMapping("/investReportListView")
	public String investReportListView(SearchParam param,ModelMap model) {
		List<ReportInfoVo> announceList = reportInfoService.getReportInfoList("ANNOUNCE_REPORT");//公告类型
		List<ReportInfoVo> regularList = reportInfoService.getReportInfoList("REGULAR_REPORT");//定期报告类型
		List<DictionaryVo> dicList = DictionaryCached.getDictionaryByRootType("TODO_STATUS");
		model.addAttribute("dicList",dicList);
		model.addAttribute("announceList",announceList);
		model.addAttribute("regularList",regularList);
		return JSP_PATH + "investReportList";
	}

	@RequestMapping("/investReportListPage")
	@ResponseBody
	public Page<InvestReportVo> investReportListPage(SearchParam param) {
		Page<InvestReportVo> list = investReportService.investReportListPage(param);
		return list;
	}
	
	/**
	 * 跳转到上传页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/investUpload")
	public String investUpload(@RequestParam("investRepId") String investRepId,
			@RequestParam("investUploadType") String investUploadType, ModelMap model){
		if(StringUtils.isNotBlank(investRepId)){
			InvestReportVo investReportVo = investReportService.getInvestReportById(investRepId);
			model.addAttribute("investReportVo", investReportVo);
		}
		List<AttachVo> list = investReportService.getInvestAttach(investRepId,investUploadType);
		String attachListJson = null;
		if(!CollectionUtils.isEmpty(list)) {
			attachListJson = StringUtil.changeSpecialCharacter(JSONArray.fromObject(list).toString());
		}
		
		model.addAttribute("investUploadType",investUploadType);
		model.addAttribute("attachList",attachListJson);
		
		if(StringUtils.equalsIgnoreCase(investUploadType, "INVEST_DATA")){
			model.addAttribute("tablePageName","材料上传");
		}
		if(StringUtils.equalsIgnoreCase(investUploadType, "INVEST_FILE")){
			model.addAttribute("tablePageName","报告上传");
		}
		
		return JSP_PATH + "uploadInvestReport";
	}
	
	/**
	 * 保存投资者报告
	 * @param documentVo
	 * @return
	 */
	@RequestMapping("/saveInvestReport")
	public String saveInvestReport(InvestReportVo investReportVo){
		//绑定附件-资源关联关系
		String attIds = investReportVo.getAttIds();
		String investRepId = investReportVo.getInvestRepId();//投资者报告ID
		String investUploadType = investReportVo.getInvestUploadType();
		
		if(StringUtils.isNotBlank(investRepId)){//更新投资者报告
			
		}else{//新增投资者报告
			//investReportService.insertInvestReport(investReportVo);
		}
		
		investRepId = investReportVo.getInvestRepId();
		if(StringUtils.isNotBlank(investRepId) && StringUtils.isNotBlank(attIds)){//保存附件关联关系表
			investReportService.addAttachRel(attIds,investUploadType,investRepId);
		}
		
		return redirectSuccess();
	}

	/**
	 * 批量下载（压缩包）
	 * @param addIdStr
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/batchDownAttach")
	public void batchDownAttach(@RequestParam("investRepIds") String investRepIds,
			@RequestParam("investUploadType") String investUploadType,
			@RequestParam("downType") String downType,
			HttpServletRequest request,HttpServletResponse response) throws IOException {
		log.info("投资者报告批量下载，investRepIds:" + investRepIds);
		//判断前端传递过来的业务id是否为空
		investReportService.downloadAttachZip(investRepIds,investUploadType,downType, response, request);
	}
	
	/**
	 * 批量下载（压缩包）
	 * @param addIdStr
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/batchDownAttachByType")
	public String batchDownAttachByType(@RequestParam("reportType") String reportType,
			@RequestParam("reprotDateBgn") String reprotDateBgn,
			@RequestParam("reprotDateEnd") String reprotDateEnd,
			HttpServletRequest request,HttpServletResponse response) throws IOException {
		//通过报告类型+报告时间查询报告
		List<InvestReportVo> list = investReportService.getInvestByType(reportType, reprotDateBgn, reprotDateEnd);
		String investRepIds = "";
		if(null != list){
			for(InvestReportVo investReportVo : list){
				investRepIds += investReportVo.getInvestRepId() + ",";
			}
		}
		return "redirect:/business/myHome/todoAndOutput/batchEndFileDownload4Biz.do?bizType=INFO_DIS_INVESTER&bizId=" + investRepIds;
	}
	
	//校验报告附件是否存在
	@RequestMapping("/checkAttachFile")
	@ResponseBody
	public Object checkAttachFile(@RequestParam(value="reportType",required = false) String reportType,
			@RequestParam(value="reprotDateBgn",required = false) String reprotDateBgn,
			@RequestParam(value="reprotDateEnd",required = false) String reprotDateEnd){
		String investRepIds = "";
		Result<Object> result = new Result<>(false);
		//通过报告类型+报告时间查询报告
		List<InvestReportVo> list = investReportService.getInvestByType(reportType, reprotDateBgn, reprotDateEnd);
		if(CollectionUtils.isNotEmpty(list)){
			for(InvestReportVo investReportVo : list){
				investRepIds += investReportVo.getInvestRepId() + ",";
			}
		
			List<TodoBizRelVo> relVos = todoService.queryRelVoByBiz("INFO_DIS_INVESTER", investRepIds);
			if (CollectionUtils.isNotEmpty(relVos)) {
				List<String> idList = new ArrayList<>();
				for (TodoBizRelVo relVo : relVos) {
					idList.add(relVo.getTodoId());
				}
				String ids = Joiner.on(",").join(idList);
				List<TodoVo> todoVos = todoService.queryTodoOutFiles(ids);
				if (CollectionUtils.isNotEmpty(todoVos)) {
					result.setSuccess(true);
				}
			}
		}
		
		return result;
	}
}
